package com.itjin.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itjin.model.domain.entity.DialogLog;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

/**
 * 对话记录Mapper接口
 */
@Mapper
public interface DialogLogMapper extends BaseMapper<DialogLog> {

  /**
   * 根据ID查询对话记录（带明确的结果映射）
   */
  @Select("SELECT * FROM dialog_logs WHERE id = #{id} AND isDelete = 0")
  @Results({
      @Result(property = "id", column = "id"),
      @Result(property = "userId", column = "userId"),
      @Result(property = "dialogContents", column = "dialogContents"),
      @Result(property = "dialogAsk", column = "dialogAsk"),
      @Result(property = "emotionAnalysis", column = "emotionAnalysis", jdbcType = JdbcType.VARCHAR, typeHandler = com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler.class),
      @Result(property = "dialogType", column = "dialogType"),
      @Result(property = "createTime", column = "createTime"),
      @Result(property = "updateTime", column = "updateTime"),
      @Result(property = "isDelete", column = "isDelete")
  })
  DialogLog getDialogWithEmotionById(@Param("id") Integer id);

  /**
   * 获取用户的对话历史记录
   */
  @Select("SELECT * FROM dialog_logs WHERE userId = #{userId} AND isDelete = 0 ORDER BY createTime DESC")
  @Results({
      @Result(property = "id", column = "id"),
      @Result(property = "userId", column = "userId"),
      @Result(property = "dialogContents", column = "dialogContents"),
      @Result(property = "dialogAsk", column = "dialogAsk"),
      @Result(property = "emotionAnalysis", column = "emotionAnalysis", jdbcType = JdbcType.VARCHAR, typeHandler = com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler.class),
      @Result(property = "dialogType", column = "dialogType"),
      @Result(property = "createTime", column = "createTime"),
      @Result(property = "updateTime", column = "updateTime"),
      @Result(property = "isDelete", column = "isDelete")
  })
  List<DialogLog> getUserDialogHistory(@Param("userId") Integer userId);
}